﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class CateGalleryBLL
    {
        public List<CateGallery> GetAll()
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.CateGalleries.OrderBy(p => p.CreateDate).ToList();
            }
        }

        public List<CateGallery> GetAll(bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.CateGalleries.Where(p => p.IsEnable == isEnable).OrderBy(p => p.CreateDate).ToList();
            }
        }

        //Get By Parent

        public CateGallery GetById(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.CateGalleries.Where(p => p.Id == id).FirstOrDefault();
            }
        }

        public CateGallery GetById(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.CateGalleries.Where(p => p.Id == id && p.IsEnable == isEnable).FirstOrDefault();
            }
        }

        //Get max sort
       

        public void Delete(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                CateGallery obj = data.CateGalleries.Where(p => p.Id == id).FirstOrDefault();
                if (obj != null)
                {
                    data.CateGalleries.DeleteOnSubmit(obj);
                    data.SubmitChanges();
                }
            }
        }
        public void Enable(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                CateGallery obj = data.CateGalleries.Where(p => p.Id == id).FirstOrDefault();
                obj.IsEnable = isEnable;

                data.SubmitChanges();
            }
        }

        //Add

        public void Add(CateGallery obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                data.CateGalleries.InsertOnSubmit(obj);
                data.SubmitChanges();
            }
        }

        //Update

        public void Update(CateGallery obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                CateGallery category = data.CateGalleries.Where(p => p.Id == obj.Id).FirstOrDefault();

                category.CreateDate = obj.CreateDate;
                category.Name = obj.Name;
                category.IsEnable = obj.IsEnable;

                data.SubmitChanges();
            }
        }
    }
}
